package com.shuyao.system.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuyao.system.common.Result;
import com.shuyao.system.common.constant.CommonConstant;
import com.shuyao.system.domain.entity.SysUser;
import com.shuyao.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @Author yuzhiqin
 * @since 2024-01-20
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class SysUserController {

	@Autowired
	private ISysUserService sysUserService;

    /**
     * 获取租户下用户数据（支持租户隔离）
     * @param user
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<IPage<SysUser>> queryPageList(SysUser user, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {

		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        return sysUserService.queryPageList(req, queryWrapper, pageSize, pageNo);
	}


	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result<SysUser> add(@RequestBody SysUser sysUser) {
		Result<SysUser> result = new Result<SysUser>();
		try {
			sysUserService.save(sysUser);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败");
		}
		return result;
	}
}
